<template>
  <web-view :src="src" @message="bindMessage"></web-view>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  data() {
    return {
      src: '',
      shareParams: {},    //  分享参数
      type: '',   //  healthWX:健康社区
    }
  },
  components: {
  },
  computed: {
    ...mapGetters(['location', 'scopeId', 'storeId', 'wxID', 'userInfo', 'openId', "userPhone", "brandId", "phone", "sharePhone", "employeeType"]),
  },
  methods: {
    ...mapActions(['updateAppSetting', "openBrandByWebview", "openGoodsDetailByWebview"]),
    bindMessage(e) {
      console.log('bindMessage', e)
      const obj = e.mp.detail.data.pop() || {};
      if (obj.type && obj.type == 'loginOut') {
        console.log('调取退出登录接口')
        // 调取退出登录接口
        this.$api.getToken({
            uri:"/myInfo/logout"
          }).then(() => {
        console.log('退出登录 token')

          this.$api.logout().then(res => { 
            // 顺便把userInfo置为空
        console.log('退出登录')

          })
        })
          return;
      }
      this.shareParams = e.mp.detail.data.pop() || {};
    }
  },
  onLoad(options) {
    // console.log(getUrlParam('type'))
    console.log(options, "web页获取渠道")
    //       if (options.brandId){
    //         this.updateAppSetting({
    //           brandId: options.brandId,
    //           phone: options.phone
    //         })
    //         console.log('onload', options)
    //         let route = '/brandMall/home'
    //         this.openBrandByWebview({
    //           route: route,
    //           jumpType: 'push',
    //           sendLocation: true
    //         })
    //         if (options.phone){
    //           let params = {
    //             phone : options.phone || "",
    //             employeeType: options.employeeType|| ""
    //           }
    //           this.$api.bindEmployee(params).then(data => {
    //           }, err => {})
    //         }
    //         return
    //       }
    if (options.productid) {
      this.updateAppSetting({
        productid: options.productid,
        phone: options.phone
      })
      // let route = `/goods/detail?id=${options.productid}&chainId=0`
      let route = `/goods/detail`
      this.openGoodsDetailByWebview({
        route: route,
        jumpType: 'push',
        sendLocation: true
      })
      if (options.phone) {
        let params = {
          phone: options.phone || "",
          employeeType: options.employeeType || ""
        }
        this.$api.bindEmployee(params).then(data => { }, err => { })
      }
      return
    }

    this.type = options.type || ''

    //  跳转分享的页面
    if (options.shareUrl) {
      // if(decodeURIComponent(options.shareUrl).indexOf('medtreecare') > -1){
      //   let src = decodeURIComponent(options.shareUrl)
      //   src = src.indexOf("&") > -1 ? src.split('&')[0] : src
      //   src = src.indexOf('?') > -1 ? src + '&' : src + '?'
      //   src += `userInfo=${this.userInfo && JSON.stringify(this.userInfo) || ''}`

      //   console.log("分享进入的页面路径",src)
      //   this.src = src

      // }else{
      this.src = decodeURIComponent(options.shareUrl) + `&openid=${this.openId}&scopeId=${this.scopeId}`
      console.log('通过分享进入页面, src:::::', this.src)
      // }

      return
    }

    //  跳转健康社区
    if (options.type == 'healthWX') {
      this.src = `${this.$env.healthWXBaseUrl}${decodeURIComponent(options.route)}`
      console.log('进入健康社区页面，src::::', this.src)
      return
    }

    //  跳转直播活动
    if (options.type == 'live') {
      this.src = `${this.$env.liveBaseUrl}${decodeURIComponent(options.route)}`
      console.log('进入直播活动页面，src::::', this.src)
      return
    }

    let wxID = this.wxID

    //  跳转支付结果页
    if (options.goPayResult == 1) {
      console.log('支付完成跳转支付结果页')

      let url = '/pay'
      // wxID = options.wxID     //  使用h5传过来的wxID
      if (options.type == 'order') {    //  普通订单
        url += `?orderId=${options.orderId}`
      }
      if (options.type == 'appointment') {    //  预约挂号订单
        url += `?ghOrderId=${options.orderId}`
      }
      if (options.type == 'groupBuy') {    //  拼团订单
        url += `?groupbuy=${options.orderId}`
      }
      if (options.type == 'pointsMall') {   //  积分商城订单
        url += `?integralsOrderId=${options.orderId}`
      }
      if (options.type == 'presc') {   //  处方商城订单
        url += `?prescId=${options.orderId}`
      }
      if (options.type == 'kouZhao') {   //  商城预约订单
        url += `?ftOrderId=${options.orderId}`
      }
      options.route = encodeURIComponent(url)
    }
    console.log('options.route', options.route)
    let backUrl = options.route ? (this.$env.backUrlBase + decodeURIComponent(options.route)) : ''
    console.log('backUrl', backUrl)
    this.src = `${this.$env.srcBase}?wxID=${wxID}&openid=${this.openId}&oauthType=WeMini&backurl=${encodeURIComponent(backUrl)}&scopeId=${this.scopeId}&storeId=${this.storeId}`
    console.log('src:::::', this.src)
    console.log('openId:::::', this.openId)
    console.log('wxID:::::', wxID)
    console.log('backUrl:::::', backUrl)
    console.log('scopeId:::::', this.scopeId)
    console.log('storeId:::::', this.storeId)
  },
  //转发设置
  onShareAppMessage(res) {
    let phone = this.sharePhone || this.userPhone || ''
    console.log(this.sharePhone, this.employeeType, 'web页转发企业渠道')
    console.log(this.userPhone, this.employeeType, 'web页转发普通渠道')
    let link = this.shareParams.link && encodeURIComponent(this.shareParams.link) || ''

    //  暂不提供健康社区分享
    if (this.type == 'healthWX') {
      link = ''
    }
    console.log("src", this.src)
    return {
      title: this.shareParams.title || '壹药臻选会员中心',
      desc: this.shareParams.desc || '',
      // path:this.src,
      path: `pages/index?shareUrl=${link}&userPhone=${phone}&employeeType=${this.employeeType}`,
      // imageUrl: this.shareParams.imgUrl || '',       //  默认使用截图
      success: () => { },
    }
  }
}
</script>
